package com.xbts.web.controller.cloud;

import java.util.LinkedList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xbts.common.annotation.Log;
import com.xbts.common.core.controller.BaseController;
import com.xbts.common.core.domain.AjaxResult;
import com.xbts.common.enums.BusinessType;
import com.xbts.system.domain.CnArticle;
import com.xbts.system.service.ICnArticleService;
import com.xbts.common.utils.poi.ExcelUtil;
import com.xbts.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * 文章Controller
 * @author xbts
 * @date 2023-08-03
 */
@Api("文章")
@RestController
@RequestMapping("/system/article")
public class CnArticleController extends BaseController {

    @Autowired
    private ICnArticleService cnArticleService;

    /**
     * 查询文章列表
     */
    @ApiOperation("查询文章列表")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody CnArticle cnArticle){
        startPage(cnArticle);
        List<CnArticle> list = cnArticleService.selectCnArticleList(cnArticle);
        return getDataTable(list);
    }

    /**
     * 导出文章列表
     */
    @ApiOperation("导出文章列表")
    @PreAuthorize("@ss.hasPermi('system:article:export')")
    @Log(title = "文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,@RequestBody CnArticle cnArticle) {
        List<CnArticle> list = cnArticleService.selectCnArticleList(cnArticle);
        ExcelUtil<CnArticle> util = new ExcelUtil<CnArticle>(CnArticle.class);
        util.exportExcel(response, list, "文章数据");
    }

    /**
     * 获取文章详细信息
     */
    @ApiOperation("获取文章详细信息")
    @ApiImplicitParam(name = "id", value = "文章ID", required = true, dataType = "String", paramType = "path", dataTypeClass=String.class)
    @PreAuthorize("@ss.hasPermi('system:article:query')")
    @GetMapping(value = "/byType/{type}")
    public AjaxResult getInfoByType(@PathVariable("type") String type) {
        return success(cnArticleService.getOne(new LambdaQueryWrapper<CnArticle>()
                .eq(CnArticle::getType, type)));
    }
    /**
     * 获取文章详细信息
     */
    @ApiOperation("获取文章详细信息")
    @ApiImplicitParam(name = "id", value = "文章ID", required = true, dataType = "String", paramType = "path", dataTypeClass=String.class)
    @PreAuthorize("@ss.hasPermi('system:article:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(cnArticleService.selectCnArticleById(id));
    }


    /**
     * 新增文章
     */
    @ApiOperation("新增文章")
    @PreAuthorize("@ss.hasPermi('system:article:add')")
    @Log(title = "文章", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody CnArticle cnArticle) {
        return toAjax(cnArticleService.insertCnArticle(cnArticle));
    }

    /**
     * 修改文章
     */
    @ApiOperation("修改文章")
    @PreAuthorize("@ss.hasPermi('system:article:edit')")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody CnArticle cnArticle) {
        return toAjax(cnArticleService.updateCnArticle(cnArticle));
    }

    /**
     * 删除文章
     */
    @ApiOperation("删除文章")
    @ApiImplicitParam(name = "ids", value = "文章ID集合", required = true, dataType = "String", paramType = "path", dataTypeClass=String.class)
    @PreAuthorize("@ss.hasPermi('system:article:remove')")
    @Log(title = "文章", businessType = BusinessType.DELETE)
	@PostMapping("/batchDelete")
    public AjaxResult batchDelete(@RequestBody List<String> ids) {
        return toAjax(cnArticleService.deleteCnArticleByIds(ids));
    }

}
